#include<iostream>
using namespace std;
#include<string>
#include<sys/socket.h>
#include<sys/types.h>
#include<cstring>
#include<arpa/inet.h>
#include<netinet/in.h>
#include<unistd.h>
class UdpClient
{
  private:
    int sockfd;
    string server_ip;
    int server_port;
  public:
    UdpClient(string ip,int port)
      :server_ip(ip)
       ,server_port(port)
    {

    }
    bool InitUdpClient()
    {
        sockfd=socket(AF_INET,SOCK_DGRAM,0);
        if(sockfd<0)
        {
          cerr<<"socket error!"<<endl;
          return 1;
        }
        //客户端不需要绑定
        return true;
    }
    void Start()
    {
      //目的地址信息
      struct sockaddr_in peer;
      memset(&peer,0,sizeof(peer));
      peer.sin_family=AF_INET;
      peer.sin_port=htons(server_port);
      peer.sin_addr.s_addr=inet_addr(server_ip.c_str());
      string msg;
      for(;;)
      {
          cout<<"please Enter#";
          cin>>msg;
          sendto(sockfd,msg.c_str(),msg.size(),0,(struct sockaddr*)& peer,sizeof(peer));
          char buffer[128];
          struct sockaddr_in temp;
          socklen_t len=sizeof(temp);
         ssize_t size=recvfrom(sockfd,buffer,sizeof(buffer)-1,0,(struct sockaddr*)& temp,&len);
         if(size>0)
         {
           buffer[size]=0;
           cout<<buffer<<endl;
         }
          
         
      }

    }
    ~UdpClient()
    {
      if(sockfd>=0)
      {
        close(sockfd);
      }
    }

};
